package wearingpartsDefinition

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/wearingpartsDefinition"
	wearingpartsDefinitionReq "github.com/flipped-aurora/gin-vue-admin/server/model/wearingpartsDefinition/request"
)

type EquipmentPartService struct{}

// CreateEquipmentPart 创建设备件记录
// Author [yourname](https://github.com/yourname)
func (EPService *EquipmentPartService) CreateEquipmentPart(ctx context.Context, EP *wearingpartsDefinition.EquipmentPart) (err error) {
	err = global.GVA_DB.Create(EP).Error
	return err
}

// DeleteEquipmentPart 删除设备件记录
// Author [yourname](https://github.com/yourname)
func (EPService *EquipmentPartService) DeleteEquipmentPart(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&wearingpartsDefinition.EquipmentPart{}, "id = ?", ID).Error
	return err
}

// DeleteEquipmentPartByIds 批量删除设备件记录
// Author [yourname](https://github.com/yourname)
func (EPService *EquipmentPartService) DeleteEquipmentPartByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]wearingpartsDefinition.EquipmentPart{}, "id in ?", IDs).Error
	return err
}

// UpdateEquipmentPart 更新设备件记录
// Author [yourname](https://github.com/yourname)
func (EPService *EquipmentPartService) UpdateEquipmentPart(ctx context.Context, EP wearingpartsDefinition.EquipmentPart) (err error) {
	err = global.GVA_DB.Model(&wearingpartsDefinition.EquipmentPart{}).Where("id = ?", EP.ID).Updates(&EP).Error
	return err
}

// GetEquipmentPart 根据ID获取设备件记录
// Author [yourname](https://github.com/yourname)
func (EPService *EquipmentPartService) GetEquipmentPart(ctx context.Context, ID string) (EP wearingpartsDefinition.EquipmentPart, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&EP).Error
	return
}

// GetEquipmentPartInfoList 分页获取设备件记录
// Author [yourname](https://github.com/yourname)
func (EPService *EquipmentPartService) GetEquipmentPartInfoList(ctx context.Context, info wearingpartsDefinitionReq.EquipmentPartSearch) (list []wearingpartsDefinition.EquipmentPart, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&wearingpartsDefinition.EquipmentPart{})
	var EPs []wearingpartsDefinition.EquipmentPart
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["ID"] = true
	orderMap["CreatedAt"] = true
	orderMap["code"] = true
	orderMap["cycle"] = true
	orderMap["usage_count"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&EPs).Error
	return EPs, total, err
}
func (EPService *EquipmentPartService) GetEquipmentPartPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
